package com.base.cn.platform.os.controller.manage.user;

import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.manage.function.SysFunction;
import com.base.cn.platform.os.entity.manage.user.SysUserFunction;
import com.base.cn.platform.os.service.manage.function.SysFunctionBiz;
import com.base.cn.platform.os.service.manage.user.SysUserFunctionBiz;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户权限Controller
 *
 * @author s.li
 * @date 2019/02/14
 */
@RestController
@RequestMapping("/manage")
@Slf4j
public class SysUserFunctionController extends BaseController {

    @Autowired
    private SysUserFunctionBiz sysUserFunctionBiz;
    @Autowired
    private SysFunctionBiz sysFunctionBiz;

    /**
     * 获取所有的权限数据
     * @param userId 用户ID
     * @return 权限数据列表
     */
    @RequestMapping("/findAllFunction")
    public List<SysFunction> findAllFunction(@RequestParam("userId") BigDecimal userId){
        //获取所有的权限数据
        List<SysFunction> sysAllFunctionList = sysFunctionBiz.find(" 1=1",null, Arrays.asList("id","functionName","functionType","parentId"));
        //获取用户角色和部门的权限数据
        List<SysFunction> userRoleGroupFunctionList = sysFunctionBiz.findUserRoleAndGroupFunction(userId);
        if(ObjectUtils.isNotEmpty(userRoleGroupFunctionList) && ObjectUtils.isNotEmpty(userRoleGroupFunctionList)){
            Map<BigDecimal,SysFunction> sysFunctionMap = userRoleGroupFunctionList.stream().collect(Collectors.toMap(e->e.getId(), e->e));
            sysAllFunctionList.forEach(e->{
                SysFunction function = sysFunctionMap.get(e.getId());
                if(ObjectUtils.isNotEmpty(function)){
                    e.setSelected(true);//选择中
                    e.setDisabled(true);//不可操作
                }
            });
        }
        //获取用户的单独授权记录
        List<SysUserFunction> userFunctionList = sysUserFunctionBiz.findUserFunctionBuyUserId(userId);
        if(ObjectUtils.isNotEmpty(userFunctionList)){
            Map<BigDecimal,SysUserFunction> userFunctionMap = userFunctionList.stream().collect(Collectors.toMap(e->e.getFunctionId(),e->e));
            sysAllFunctionList.forEach(e->{
                SysUserFunction userFunction = userFunctionMap.get(e.getId());
                if(userFunction !=null){
                    e.setSelected(true);
                    e.setDisabled(false);
                }
            });
        }
        return sysAllFunctionList;
    }

    /**
     * 保存用户权限
     * @param userId 用户ID
     * @param functionIds　权限ID串
     * @retur Map<String,Object>
     */
    @RequestMapping("/saveUserFunction")
    public Map<String,Object> saveUserFunction(@RequestParam("userId") BigDecimal userId,
                                        @RequestParam(value = "functionIds",required = false,defaultValue = "") String functionIds){
        try {
            sysUserFunctionBiz.saveUserFunction(userId,functionIds);
        } catch (Exception e) {
            log.error("saveUserFunction()--error",e);
            return ResultUtil.ERROR("系统错误，用户授权失败");
        }
        return ResultUtil.SUCCESS("用户授权成功");
    }
}
